package com.zx.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zx.sys.constast.SysConstast;
import com.zx.sys.domain.Menu;
import com.zx.sys.domain.User;
import com.zx.sys.service.MenuService;
import com.zx.sys.utils.DataGridView;
import com.zx.sys.utils.ResultObj;
import com.zx.sys.utils.TreeNode;
import com.zx.sys.utils.TreeNodeBuilder;
import com.zx.sys.utils.WebUtils;
import com.zx.sys.vo.MenuVo;

@RestController
@RequestMapping("menu")
public class MenuController {
	
	@Autowired
	private MenuService menuService;
	
	@RequestMapping("loadIndexLeftMenuJson")
	public List<TreeNode> loadIndexLeftMenuJson(MenuVo menuVo){
		User user = (User) WebUtils.getHttpSession().getAttribute("user");
		List<Menu> list = null;
		menuVo.setAvailable(SysConstast.AVAILABLE_TRUE);
		
		if(user.getType()==SysConstast.USER_TYPE_SUPER){
			list = menuService.queryAllMenuForList(menuVo);
		}else{
			list = menuService.queryMenuByUserIdForList(menuVo, user.getUserid());
		}
		
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for (Menu menu : list) {
			Integer id = menu.getId();
			Integer pid = menu.getPid();
		    String title = menu.getTitle();
			String icon = menu.getIcon();
			String href = menu.getHref();
			Boolean spread = menu.getSpread()==SysConstast.SPREAD_TRUE?true:false;
			String target = menu.getTarget();
			nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));
		}
		return TreeNodeBuilder.builder(nodes, 1);
	}
	
	@RequestMapping("loadMenuManagerLeftTreeJson")
	public DataGridView loadMenuManagerLeftTreeJson(MenuVo menuVo){
		menuVo.setAvailable(SysConstast.AVAILABLE_TRUE);
		List<Menu> list = menuService.queryAllMenuForList(menuVo);
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for (Menu menu : list) {
			Integer id = menu.getId();
			Integer pid = menu.getPid();
		    String title = menu.getTitle();
			String icon = menu.getIcon();
			String href = menu.getHref();
			Boolean spread = menu.getSpread()==SysConstast.SPREAD_TRUE?true:false;
			String target = menu.getTarget();
			nodes.add(new TreeNode(id, pid, title, icon, href, spread, target));
		}
		return new DataGridView(nodes);
	}
	
	@RequestMapping("loadAllMenu")
	public DataGridView loadAllMenu(MenuVo menuVo){
		return menuService.queryAllMenu(menuVo);
	}
	
	@RequestMapping("addMenu")
	public ResultObj addMenu(MenuVo menuVo){
		try {
			menuVo.setId(null);
			menuService.addMenu(menuVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.ADD_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.ADD_ERROR);
		}
	}
	
	@RequestMapping("updateMenu")
	public ResultObj updateMenu(MenuVo menuVo){
		try {
			menuService.updateMenu(menuVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.UPDATE_ERROR);
		}
	}
	
	@RequestMapping("checkMenuHasChildren")
	public ResultObj checkMenuHasChildren(MenuVo menuVo){
		Integer count = menuService.queryMenuByPid(menuVo.getId());
		if(count > 0){
			return new ResultObj(SysConstast.CODE_SUCCESS);
		}else{
			return new ResultObj(SysConstast.CODE_ERROR);
		}
	}
	
	@RequestMapping("deleteMenu")
	public ResultObj deleteMenu(MenuVo menuVo){
		try {
			this.menuService.deleteMenu(menuVo);
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}
}
